<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
		<link rel="stylesheet" href="../../css/common.css" media="all">
	</head>
	<body>
		<div class="layui-fluid">
			<div class="layui-row layui-col-space15">
				<div class="layui-col-md12">
					<div class="layui-card">
						<div class="layui-card-header">
							<span class="marign_right10">店铺选择</span>
						</div>
						<div class="layui-card-body">
							<!-- 搜索 -->
							<form class="layui-form search" id="form" onsubmit="return false" action="##"> 
								<div class="layui-inline search_inline">
									<label class="layui-form-label">查询下级：</label>
									<div class="layui-input-inline">
										<input class="layui-input" placeholder="查询下级" name="fid" autocomplete="off">
									</div>
								</div>
								<div class="layui-inline search_inline">
									<label class="layui-form-label">标题：</label>
									<div class="layui-input-inline">
										<input class="layui-input" placeholder="标题" name="keyword" autocomplete="off">
									</div>
								</div> 
								<button class="layui-btn search_inline" lay-filter="search" data-type="reload">搜索</button>
								<button class="layui-btn layui-btn-primary search_inline" type="reset">重置</button>
								<button class="layui-btn layui-btn-primary search_inline" onclick="openAll();">展开或折叠全部</button>
							</form>
							<!-- 搜索 -->
							<table class="layui-hide" id="test-table-operate" lay-filter="test-table-operate"></table>
							<div class="layui-form-item layui-layout-admin">
								<div class="layui-input-block">
									<div class="layui-footer" style="left: 0;">
										<button class="layui-btn" id="queding">确定</button>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>

		<script src="../../layuiadmin/layui/layui.js"></script>
		<script src="../../js/jquery.min.js"></script>
		<script src="../../js/common.js"></script>
		<style>
			.layui-card-body{
				height: 650px;
			}
			.test-table-operate{
				max-height: 600px;
			}
			.layui-table-body{
				max-height: 600px;
			}
		</style>
		<script>
			var comeData = {
				id: getParam('id'),
			}
			var editObj = null,
				treeGrid = null,
				tableId = 'test-table-operate',
				layer = null;
			layui.config({
				base: '../../layuiadmin/' //静态资源所在路径
			}).extend({
				index: 'lib/index' //主入口模块
			}).use(['index', 'table', 'form', 'treeGrid'], function() {
				var table = layui.table,
					$ = layui.$,
					form = layui.form,
					treeGrid = layui.treeGrid,
					admin = layui.admin;
				var checked = {};
				var _data = {
					member_id:cookie.GetCookie("shop_memberid"),
					btype: 0
				};
				initdata();

				function initdata() {
					treeGrid.render({
						id:'test-table-operate',
						elem: '#test-table-operate',
						url: server.shop_query,
						cellMinWidth: 100,
						idField: 'id', //必須字段
						treeId: 'id', //树形id字段名称
						treeUpId: 'pid', //树形父id字段名称
						treeShowName: 'sub_shopname', //以树形式显示的字段
						iconOpen: true, //是否显示图标【默认显示】
						isOpenDefault: true, //节点默认是展开还是折叠【默认展开】
						loading: true,
						method: 'post',
						height: 'full-150',
						cols: [
							[{
								type: 'radio',
								fixed: 'left',
								width: 50,
							}, {
								type: 'numbers',
								width: 50,
								title: '#',
							}, {
								field: 'sub_shopname',
								width: 200,
								title: '子店铺名',
								style:'text-align:left;'
							}, {
								field: 'sub_shopid',
								title: '子店铺ID',
								width: 100,
							}, {
								field: 'shopname',
								title: '主店铺名',
								width: 100,
								style:'text-align:left;'
							}, {
								field: 'shopid',
								title: '主店铺ID',
								width: 100,
							}, {
								field: 'logo',
								width: 60,
								title: 'logo',
								templet: function(d) {
									return d.logo ? '<img src="'+d.logo+'" style="width:22px;height:22px"/>' : ''
								}
							}, {
								field: 'mobile',
								width: 120,
								title: '创建人',
							}, {
								field: 'content',
								width: 200,
								title: '店铺简介',
								style:'text-align:left;'
							}, {
								field: 'address',
								width: 280,
								title: '店铺地址',
								style:'text-align:left;'
							}, {
								field: 'main_memberid',
								width: 100,
								title: '主管理员',
							}, {
								field: 'addtime',
								width: 160,
								title: '创建时间',
							}]
						],
						page: true,
						limit: limit,
						limits: limits,
						where: _data,
						request: {
							pageName: 'page',
							limitName: 'size',
						},
						parseData: function(res) { //数据加载后回调
							let data = res.shop.data
							let newarr = new Array;
							for(let i in data){
								if(data[i].shopid == data[i].sub_shopid){
									newarr.push(data[i]);
								}
								if(data[i].sub_shopid == comeData.id){
									data[i].checked = true;
								}
							}
							for(let i in data){
								for(let k in newarr){
									if(data[i].shopid == newarr[k].sub_shopid){
										if(data[i].shopid != data[i].sub_shopid) data[i].pid = newarr[k].id;
									}
								}
							}
							return {
								"code": res.code, //解析接口状态
								"msg": res.msg, //解析提示文本
								"count": res.shop.pager.total, //解析数据长度
								"data": data, //解析数据列表
							};
						},
						onRadio: function(obj) { //单选事件
							checked = obj;
						},
						onClickRow: function(index,obj){
							checked = obj;
						},
						onDblClickRow:function(index,obj){
							checked = obj;
							parent.$('input[name=shopid]').val(checked.sub_shopid);
							parent.$('div[name=shopname]').html(checked.sub_shopname);
							localStorage.setItem('pxy_shopid', checked.sub_shopid);
							localStorage.setItem('pxy_shopname', checked.sub_shopname);
							parent.layer.msg("正在为您切换店铺。。。");
							parent.location.reload();
							parent.layer.closeAll();
						}
					});
				}
				$('#queding').on('click',function(){
					parent.$('input[name=shopid]').val(checked.sub_shopid);
					parent.$('div[name=shopname]').html(checked.sub_shopname);
					localStorage.setItem('pxy_shopid', checked.sub_shopid);
					localStorage.setItem('pxy_shopname', checked.sub_shopname);
					parent.layer.msg("正在为您切换店铺。。。");
					parent.location.reload();
					parent.layer.closeAll();
				});
				//监听搜索
				form.on('submit(search)', function(data) {
					_data.fid = data.field.fid
					Object.assign(_data, data.field)
					initdata();
					return false;
				});
				
				$("button[type=reset]").on("click", function() {
					_data = {}
					initdata();
					document.getElementById("form").reset();
				});
				
				window.openAll = function() {
					var treedata = treeGrid.getDataTreeList(tableId);
					treeGrid.treeOpenAll(tableId, !treedata[0][treeGrid.config.cols.isOpen]);
				}
				$('.test-table-operate-btn .layui-btn').on('click', function() {
					var type = $(this).data('type');
					active[type] ? active[type].call(this) : '';
				});
			});
		</script>
	</body>
</html>
